﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Posts_History : BasePostPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        gridHistory.DataBound += new EventHandler(gridHistory_DataBound);
        dsHistory.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Acms.Bll.Post>>(dsHistory_SelectData);
        if (!IsPostBack)
        {
            if (CurrentPost != null)
            {
                PostGuid = CurrentPost.PostGuid;
            }
        }
    }

    void gridHistory_DataBound(object sender, EventArgs e)
    {
        var item = gridHistory.MasterTableView.FindItemByKeyValue("Id", PostId);
        if (item != null)
        {
            item.Selected = true;
        }
    }

    private Guid PostGuid;

    void dsHistory_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Post> e)
    {
        e.SetData(Acms.Bll.Post.GetAllByPostGuid(PostGuid));
    }

    protected override string PageTitle
    {
        get { return "History"; }
    }

    public override Acms.Bll.Role.Roles[] AllowedRoles
    {
        get { return new Acms.Bll.Role.Roles[] { Acms.Bll.Role.Roles.Developer, Acms.Bll.Role.Roles.ContentEditor, Acms.Bll.Role.Roles.ContentViewer, Acms.Bll.Role.Roles.ContentPublisher}; }
    }

    protected override object SaveViewState()
    {
        ViewState["PostGuid"] = PostGuid;
        return base.SaveViewState();
    }

    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        PostGuid = (Guid)ViewState["PostGuid"];
    }

    protected void lnkPreview_DataBinding(object sender, EventArgs e)
    {
        var lnkPreview = sender as HyperLink;
        if (lnkPreview != null)
        {
            Telerik.Web.UI.GridDataItem dataItem = lnkPreview.NamingContainer as Telerik.Web.UI.GridDataItem;
            if (dataItem != null)
            {
                var post = dataItem.DataItem as Acms.Bll.Post;
                lnkPreview.NavigateUrl = string.Concat("http://", AcmsContext.SiteRoot, "/", post.Url.Substring(2));
            }
        }
    }

    protected void lnkDisplayName_DataBinding(object sender, EventArgs e)
    {
        var lnkDisplayName = sender as HyperLink;
        if (lnkDisplayName != null)
        {
            Telerik.Web.UI.GridDataItem dataItem = lnkDisplayName.NamingContainer as Telerik.Web.UI.GridDataItem;
            if (dataItem != null)
            {
                var post = dataItem.DataItem as Acms.Bll.Post;
                lnkDisplayName.NavigateUrl = string.Concat("~/Posts/Details.aspx?Id=", post.Id, "&SV=", post.SiteVersion != null ? Server.UrlEncode(post.SiteVersion.Value.ToString("yyyy/MM/dd HH:mm")) : null);
                lnkDisplayName.Text = post.DisplayName;
            }
        }
    }
}
